package com.gr.springboot_shiro.service.impl;

import com.gr.springboot_shiro.dao.UserDao;
import com.gr.springboot_shiro.entity.Perms;
import com.gr.springboot_shiro.entity.User;
import com.gr.springboot_shiro.service.UserService;
import com.gr.springboot_shiro.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Katakuri
 */
@Service("userService")
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    public UserDao userDao;

    @Override
    public void registerUser(User user) {
        String salt = SaltUtils.getSalt(5);
        user.setSalt(salt);
        //对密码加密
        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
        System.out.println(md5Hash.toHex());
        user.setPassword(md5Hash.toHex());
        userDao.registerUser(user);
    }

    @Override
    public User findByUserName(String userName) {
        return userDao.findByUserName(userName);
    }

    @Override
    public User findRolesByUserName(String username) {
        return userDao.findRolesByUserName(username);
    }

    @Override
    public List<Perms> findPermsByRoleId(int id) {
        return userDao.findPermsByRoleId(id);
    }
}
